<?php

// +----------------------------------------------------------------------
// | ThinkAdmin
// +----------------------------------------------------------------------
// | 版权所有 2014~2022 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
// +----------------------------------------------------------------------
// | 官方网站: https://thinkadmin.top
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// | 免费声明 ( https://thinkadmin.top/disclaimer )
// +----------------------------------------------------------------------
// | gitee 代码仓库：https://gitee.com/zoujingli/ThinkAdmin
// | github 代码仓库：https://github.com/zoujingli/ThinkAdmin
// +----------------------------------------------------------------------

namespace app\api\controller\wallpaper;

use app\api\controller\Base;
use \think\facade\Db;

/**
 * 小鲸鱼
 */
class Image extends Base
{
    protected $table = 'app_wallpaper';

    /**
     * 有课登录
     */
    public function index()
    {
        $tagArr = [1=>'付费'];
        $where = [];
        $isSelected = input('is_selected',0);
        if($isSelected == 1){
            $where[] = ['is_selected','=',1];
        }
        $categoryId = input('category_id',0);
        if($categoryId > 0){
            $where[] = ['category_id','=',$categoryId];
        }
        $categoryId = input('tag_id',0);
        if($categoryId > 0){
            $where[] = ['tag_id','=',$categoryId];
        }
        $orderString = "id desc";
        if($isSelected == 1){
            $orderString = "sort desc,zan_num desc,id desc";
        }
        $query = Db::table($this->table)
            ->field('id,title,tag_id,url,thumbnail,mid_image,zan_num,price')
            ->order($orderString)
            ->where($where);
        $whereKey = md5(json_encode($where));
        $cacheTotal = cache($whereKey);
        if($cacheTotal === NULL){
            $total = $query->count();
            cache($whereKey,$total,3600);
        }else{
            $total = (int)$cacheTotal;
        }
        $res = $query
            ->paginate(10,$total)
            ->toArray();
        $data = $res['data'];
        foreach($data as &$value){
            $value['tag_name'] = $tagArr[$value['tag_id']]??'';
            $value['txt'] = '无需看广告';
        }
        $res['data'] = $data;
        $this->success('success', $res);
    }
    public function rand(){
        $randArr = [];
        for($i=0;$i<10;$i++){
            $randArr[] = mt_rand(1,783);
        }
        $list = Db::table($this->table)->field('id,title,tag_id,url,thumbnail,mid_image,zan_num,price')->where([['id','in',$randArr]])->select()->toArray();
        foreach($list as &$value){
            $value['txt'] = '无需看广告';
        }
        $this->success('success',$list);
    }

    public function detail(){
        $id = input('id',0);
        if(empty($id)){
            $this->error("invalid id");
        }
        $row = find($this->table,'id,title,tag_id,url,thumbnail,mid_image,zan_num,price',['id'=>$id]);
        if(empty($row)){
            $this->error("id nat exist");
        }
        $this->success("success",$row);
    }

    /**
     * 点赞
     * @return void
     */
    public function zan(){
        $id = input("id",0);
        if(empty($id)){
            $this->error("invalid param");
        }
        Db::table($this->table)->where(["id"=>$id])->inc("zan_num",mt_rand(1,15))->save();
        $this->success("success");
    }
}
